package com.ita.parser;

import com.ita.entity.Customer;
import com.ita.service.CustomerService;
import com.ita.util.PrintHelper;
import com.ita.util.StringHelper;

/***
 * middle layer
 * @author Elvis
 *
 */
public class CmdParser {

	private CustomerService cService = new CustomerService();

	public void processList(String s) {

		s = s.toUpperCase();
		if (s.equals("L")) {
			PrintHelper.printCustomers(cService.findAllCustomer());
		} else if (s.contains("ID")) {
			String id = s.substring(s.indexOf(":") + 1, s.length());
			PrintHelper.printCustomers(cService.findById(id));
		} else if (s.contains(",")) {
			String name = s.split(",")[0].split(":")[1];
			String sex = s.split(",")[1].split(":")[1];
			PrintHelper.printCustomers(cService.findByNameAndSex(name, sex));
		} else {
			System.out.println("format not right!!");
		}

	}

	public void processAdd(String s) {

		Customer c = new Customer();
		c.setId(StringHelper.getRandomId());
		c.setName(StringHelper.getFiveParams(s)[0]);
		c.setSex(StringHelper.getFiveParams(s)[1]);
		c.setBirthDay(StringHelper.getFiveParams(s)[2]);
		c.setAddress(StringHelper.getFiveParams(s)[3]);
		c.setPhone(StringHelper.getFiveParams(s)[4]);
		cService.addCustomer(c);
	}

	public void processDel(String s) {

		String id = s.split(" ")[1];
		cService.deleteCustomer(id);
	}

	public void processUpdate(String s) {
		String id = s.split(":")[0].split(" ")[1];
		Customer c = cService.findById(id)[0];
		if (s.toUpperCase().contains(",")) {
			String[] arr = StringHelper.getFiveParams(s);
			Customer newC = new Customer(c.getId(), arr[0], arr[1], arr[2], arr[3], arr[4]);
			cService.delAndModCustomer(c, newC);
		} else if (s.toUpperCase().contains("NAME:")) {
			Customer newC = c;
			newC.setName(s.split(":")[1]);
			cService.delAndModCustomer(c, newC);
		} else {
			System.out.println("format not right!!");
		}

	}
	
	public void processSort(String s){
		PrintHelper.printCustomers(cService.sort());
	}

}
